package coml.jiyun.hp.day03weinfo.base;

import android.view.View;

import java.util.ArrayList;

public abstract class BasePresenter<V extends BaseView> {

    private ArrayList<BaseModel> baseModels;
    private V mView;

    public BasePresenter(){
        initModel();
    }

    protected abstract void initModel();

    //持有V层对象
    public void bindView(V view) {
        mView = view;
    }

    public void addModel(BaseModel model){

        if (baseModels == null){
            baseModels = new ArrayList<>();
        }
        baseModels.add(model);
    }

    public void onDestroy() {
        //将持有v层的对象释放，避免内存泄漏
        mView = null;
        //通知m层取消网络请求,需要拿到m层的对象
        if (baseModels != null && baseModels.size()>0){
            for (int i = 0; i < baseModels.size(); i++) {
                baseModels.get(i).destroy();
            }
        }
    }
}
